const http = require('http');
const fs = require('fs');
const path = require('path');

// 测试手机号
const phoneNumber = '19852725830';

// 测试多个可能的API路径和方法组合
const testCases = [
  {
    name: '原始GET - /auth/sendCode/{phone}',
    path: `/auth/sendCode/${phoneNumber}`,
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'isToken': 'false'
    }
  },
  {
    name: '原始POST - /auth/sendCode/{phone}',
    path: `/auth/sendCode/${phoneNumber}`,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'isToken': 'false'
    }
  },
  {
    name: 'login.js中的POST - /sendCode/{phone}',
    path: `/sendCode/${phoneNumber}`,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'isToken': 'false'
    }
  }
];

console.log('=== 开始详细测试验证码发送功能 ===');
console.log('测试时间:', new Date().toLocaleString());
console.log('测试手机号:', phoneNumber);
console.log('============================\n');

// 创建日志文件
const logFilePath = path.join(__dirname, 'sms-test-log.txt');
let logContent = `=== 验证码发送测试日志 ===\n测试时间: ${new Date().toLocaleString()}\n测试手机号: ${phoneNumber}\n============================\n\n`;

// 执行测试用例
async function runTests() {
  for (let i = 0; i < testCases.length; i++) {
    const testCase = testCases[i];
    console.log(`\n[测试 ${i+1}/${testCases.length}] ${testCase.name}`);
    logContent += `\n[测试 ${i+1}/${testCases.length}] ${testCase.name}\n`;
    
    await new Promise((resolve) => {
      const options = {
        hostname: 'localhost',
        port: 8080,
        path: testCase.path,
        method: testCase.method,
        headers: testCase.headers
      };
      
      console.log('请求URL:', `http://${options.hostname}:${options.port}${options.path}`);
      console.log('请求方法:', options.method);
      console.log('请求头:', JSON.stringify(options.headers));
      
      logContent += `请求URL: http://${options.hostname}:${options.port}${options.path}\n`;
      logContent += `请求方法: ${options.method}\n`;
      logContent += `请求头: ${JSON.stringify(options.headers)}\n`;
      
      const req = http.request(options, (res) => {
        let responseBody = '';
        
        console.log(`状态码: ${res.statusCode}`);
        console.log('响应头:', JSON.stringify(res.headers));
        
        logContent += `状态码: ${res.statusCode}\n`;
        logContent += `响应头: ${JSON.stringify(res.headers)}\n`;
        
        res.on('data', (chunk) => {
          responseBody += chunk;
        });
        
        res.on('end', () => {
          console.log('响应体:', responseBody);
          logContent += `响应体: ${responseBody}\n`;
          
          // 解析响应体（如果是JSON）
          try {
            const jsonData = JSON.parse(responseBody);
            console.log('解析后JSON:', JSON.stringify(jsonData, null, 2));
            logContent += `解析后JSON: ${JSON.stringify(jsonData, null, 2)}\n`;
          } catch (e) {
            console.log('无法解析为JSON:', e.message);
            logContent += `无法解析为JSON: ${e.message}\n`;
          }
          
          logContent += '------------------------\n';
          resolve();
        });
      });
      
      req.on('error', (e) => {
        console.error(`请求错误: ${e.message}`);
        logContent += `请求错误: ${e.message}\n------------------------\n`;
        resolve();
      });
      
      // 结束请求
      req.end();
    });
  }
  
  // 保存日志
  fs.writeFileSync(logFilePath, logContent);
  console.log('\n测试完成！详细日志已保存至:', logFilePath);
}

runTests().catch(err => {
  console.error('测试执行错误:', err);
});